﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TotalSystems.DataAccess;
using TotalSystems.Business.Public;

namespace TotalSystems.Business.Store
{
    public class StoreOrderRepository
    {   
        TSContext _context = new TSContext();
        FinancialYearRepository financialYearRepository = new Public.FinancialYearRepository();

        #region Select
        public IEnumerable<STR_StoreDocument> GetStoreDocument()
        {
            return _context.STR_StoreDocument;
        }


        public STR_StoreDocument GetMoveStoreDocument(STR_StoreDocument sTR_StoreDocument, Move move)
        {
            STR_StoreDocument q = null;
            switch (move)
            {
                case Move.First:
                    q = _context.STR_StoreDocument.Min();
                    break;
                case Move.Preview:
                    q = _context.STR_StoreDocument.Where(x => x.STR_Sd_ID <= sTR_StoreDocument.STR_Sd_ID).Max();
                    break;
                case Move.Next:
                    q = _context.STR_StoreDocument.Where(x => x.STR_Sd_ID >= sTR_StoreDocument.STR_Sd_ID).Min();
                    break;
                case Move.Last:
                    q = _context.STR_StoreDocument.Max();
                    break;
            }
            return q ;
        }

        #endregion

        #region Insert

        public void InsertStoreDocument(STR_StoreDocument sTR_StoreDocument)
        {
            sTR_StoreDocument.ACC_Fy_ID_fk = financialYearRepository.GetACC_FinancialYearActive().Select(x => x.ACC_Fy_ID_fk).First();
            _context.AddToSTR_StoreDocument(sTR_StoreDocument);
            _context.SaveChanges();
        }

        #endregion

        #region Update

        public void UpdateStoreDocument(STR_StoreDocument sTR_StoreDocument)
        {
            var q = _context.STR_StoreDocument.FirstOrDefault(x => x.STR_Sd_ID == sTR_StoreDocument.STR_Sd_ID);
            _context.STR_StoreDocument.ApplyCurrentValues(q);
            _context.SaveChanges();
        }

        #endregion
    }   
}
